#!/usr/bin/env python
# -*- coding: utf-8 -*-

from jujud.packet import Packet,depack,enpack
from jujud.tcptube import TCPChannel

import jupacket

import  log

class   CacheClient(object) :
    def __init__(self, service, server_addresses) :
        self.service = service
        self.server_addresses = server_addresses
        self._channels = []

        for address in server_addresses :
            channel = TCPChannel(address)
            self._channels.append(channel)
        # __init__()

    def __del__(self) :
        for channel in self._channels :
            del channel
        self._channels = []
        # __del__()
        
    def getChannel(self, key) :
        if len(self._channels) > 1 :
            raise NotImplementedError
        return  self._channels[0]
        # getChannel()

    def insert(self, key, data, timeout=0) :
        ju_request = jupacket.Packet()
        ju_request.setRequest()
        ju_request.setInsertAction()
        ju_request.key = key
        ju_request.data = data
        ju_request.timeout = timeout
        body = jupacket.encodeRequest(ju_request)

        pkt = Packet()
        pkt.service = self.service
        pkt.body = body
        pkt.setRequest()
        request, packid = enpack(pkt)

        channel = self.getChannel(key)
        channel.sendRecord(request)

        response = channel.recvRecord()
        if not response :
            raise RuntimeError,'Fail to response to INSERT(key=%s data=%s timeout=%d)' % (`key`, `data`, timeout)
        pkt = depack(response)
        body = pkt.body
        ju_response = jupacket.decodeResponse(body)

        return ju_response.status
        # insert()

    def erase(self, key) :
        ju_request = jupacket.Packet()
        ju_request.setRequest()
        ju_request.setEraseAction()
        ju_request.key = key
        body = jupacket.encodeRequest(ju_request)

        pkt = Packet()
        pkt.service = self.service
        pkt.body = body
        pkt.setRequest()
        request, packid = enpack(pkt)

        channel = self.getChannel(key)
        channel.sendRecord(request)

        response = channel.recvRecord()
        if not response :
            raise RuntimeError, 'Fail to response to ERASE(key=%s)' % `key`
        pkt = depack(response)
        body = pkt.body
        ju_response = jupacket.decodeResponse(body)

        return ju_response.status
        # erase()

    def lookup(self, key) :
        ju_request = jupacket.Packet()
        ju_request.setRequest()
        ju_request.setLookupAction()
        ju_request.key = key
        body = jupacket.encodeRequest(ju_request)

        pkt = Packet()
        pkt.service = self.service
        pkt.body = body
        pkt.setRequest()
        request, packid = enpack(pkt)

        channel = self.getChannel(key)
        channel.sendRecord(request)

        response = channel.recvRecord()
        if not response :
            raise RuntimeError, 'Fail to response to LOOKUP(key=%s)' % `key`
        pkt = depack(response)
        body = pkt.body
        ju_response = jupacket.decodeResponse(body)

        return ju_response.status,ju_response.data
        # lookup()

    # class CacheClient
